package com.wanhe.base.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by 陈德基 on 2016/8/17.
 */
public class StringFileUtil {

    private static final String TAG = "StringFileUtil";

    /**
     * 读取文件中的字符串
     * @author 陈德基
     * create at 2017/4/19 15:44
     */
    public static String readFromFile(File src) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(
                    src));
            StringBuilder stringBuilder = new StringBuilder();
            String content;
            while ((content = bufferedReader.readLine()) != null) {
                stringBuilder.append(content);
            }
            return stringBuilder.toString();
        } catch (FileNotFoundException e) {
            Logger.e(TAG, "文件找不到");
            return null;
        } catch (IOException e) {
            Logger.e(TAG, "文件读取失败");
            return null;
        }
    }

    /**
     * File Name:StringFileUtil.java<br/>
     * Package Name:com.wanhe.base.util<br/>
     * Date:2017/7/3 14:08<br/>
     * author:chendeji<br/>
     * desc: 将资产文件夹下的配置文件读进内存<br/>
     * Copyright (c) 2017/7/3, 781571323@qq.com All Rights Reserved.
     *
     */
    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }

    /**
     * 将文本写入文件
     * @author 陈德基
     * create at 2017/4/19 15:44
     */
    public static void writeString2File(String data, File path) {
        try {
            if (!path.exists()){
                path.createNewFile();
            }
            FileWriter writer = new FileWriter(path, false);
            writer.write(data);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            Logger.e(TAG, e.getMessage());
        }
    }

    /**
     * 判断文件是否存在
     *
     * @param destFileName
     * @return
     */
    public static File isFileExists(String directory, final String destFileName) {
        File fd = new File(directory);
        if (fd.isDirectory()){
            File[] fileNames = fd.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String filename) {
                    if (filename.startsWith(destFileName)){
                        return true;
                    }
                    return false;
                }
            });
            if (fileNames == null || fileNames.length == 0){
                return null;
            }
            return fileNames[0];
        } else {
            File[] fileNames = fd.getParentFile().listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String filename) {
                    if (destFileName.startsWith(filename)){
                        return true;
                    }
                    return false;
                }
            });
            if (fileNames == null || fileNames.length == 0){
                return null;
            }
            return fileNames[0];
        }
    }



}
